package com.cc.controller;


import com.cc.model.EbProductCategory;
import com.cc.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") int page,
                                   @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {

        List<EbProductCategory>productCategoryes = productCategoryService.list(page, pageSize);
        int maxPage = productCategoryService.getMaxPage(pageSize);

        ModelAndView mv = new ModelAndView();
        mv.addObject("productCategoryes",productCategoryes);
        mv.addObject("curPage", page);

        mv.addObject("maxPage", maxPage);
        mv.setViewName("manage/productCategory");
        return mv;
    }

    @RequestMapping("/to_save")
    public String to_Save() {
        return "manage/productCategory-add";
    }

    @RequestMapping("/save_commit")
    public String saveCommit(EbProductCategory ebProductCategory) {

        if ((ebProductCategory.getEpcName()==null || ebProductCategory.getEpcName().trim().length()==0)) {
            return "manage/productCategory-error";//error.jsp
        }
        int result = productCategoryService.save(ebProductCategory);
        //将是否添加成功的视图，告诉中央控制器
        if (result <= 0) {
            return "manage/productCategory-error";
        }
        return "manage/productCategory-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer epcId){
        //1、根据新闻的编号查询新闻的详细信息
        EbProductCategory ebProductCategory = productCategoryService.selectByCondition(epcId);
        model.addAttribute("ebProductCategory",ebProductCategory);
        System.out.println("ebProductCategory:"+ebProductCategory);
        return "manage/productCategory-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbProductCategory ebProductCategory){
        //1、更新分类信息
        int result = productCategoryService.updateById(ebProductCategory);
        //2、跳转到更新结果页面
        if(result <=0){
            return "manage/manage-error";
        }
        return "manage/productCategory-result";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1、参数校验

        //2、执行删除(真实项目中 delete from 表名 where id=?)
        int result = productCategoryService.deleteByPk(id);
        if(result==0){

        }
        return "manage/productCategory-result";
    }
}